//
//  HXNewMessageNotification.m
//  HuiXin
//
//  Created by 张毓庆 on 14-1-8.
//  Copyright (c) 2014年 HuiXin Inc. All rights reserved.
//

#import "HXNewMessageNotification.h"

@implementation HXNewMessageNotification
single_implementation(HXNewMessageNotification)
- (id)init
{
    self = [super init];
    if (self)
    {
        _player = [[AVAudioPlayer alloc] init];
        _sound = [self loadSound:@"sound.aiff"];
        [self prepAudio];
    }
    return self;
}

- (BOOL) prepAudio
{
	NSError *error;
	NSString *path = [[NSBundle mainBundle] pathForResource:@"newMessage" ofType:@"mp3"];
	if (![[NSFileManager defaultManager] fileExistsAtPath:path]) return NO;
	_player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
	if (!_player)
	{
		NSLog(@"Error: %@", [error localizedDescription]);
		return NO;
	}
	[_player prepareToPlay];
	[_player setNumberOfLoops:1];
	return YES;
}

// 加载音效
- (SystemSoundID)loadSound:(NSString *)soundFileName
{
    NSString *path = [[NSBundle mainBundle]pathForResource:soundFileName ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];

    SystemSoundID soundId;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundId);
    
    return soundId;
}


- (void)playAudio
{
//    if ([_player isPlaying])
//    {
//        [_player stop];
//    }
//    [_player play];
    
    AudioServicesPlaySystemSound(_sound);
}

- (void)playVibrate
{
    // 振动的代码
//    AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
@end
